﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKArch.Ramp
{
	public class ExtensibleStorageManager
	{
		public static void WriteRampExtensibleStorage(Floor ramp, List<double> widthList)
		{
			Guid guid = new Guid(ExtensibleStorageManager.m_WidthGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("HY_Ramp_ESData");
				schemaBuilder.AddArrayField("WidthList", typeof(double)).SetUnitType(0);
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("WidthList");
            entity.Set<IList<double>>(field, widthList, (Autodesk.Revit.DB.DisplayUnitType)6);
			ramp.SetEntity(entity);
		}

		public static bool ReadRampExtensibleStorage(Floor ramp, ref List<double> widthList)
		{
			Schema schema = Schema.Lookup(new Guid(ExtensibleStorageManager.m_WidthGuid));
			if (schema == null)
			{
				return false;
			}
			Field field = schema.GetField("WidthList");
			if (field == null)
			{
				return false;
			}
			Entity entity = ramp.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return false;
			}
            foreach (double item in entity.Get<IList<double>>(field, (DisplayUnitType)6))
			{
				widthList.Add(item);
			}
			return true;
		}

		public static void WriteRampVertRadius(Floor ramp, List<double> lstVertRadius)
		{
			Guid guid = new Guid(ExtensibleStorageManager.m_VertRadiusGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("HY_Ramp_ESData");
				schemaBuilder.AddArrayField("VertRadiusList", typeof(double)).SetUnitType(0);
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("VertRadiusList");
            entity.Set<IList<double>>(field, lstVertRadius, (Autodesk.Revit.DB.DisplayUnitType)6);
			ramp.SetEntity(entity);
		}

		public static bool ReadRampVertRadius(Floor ramp, ref List<double> lstVertRadius)
		{
			Schema schema = Schema.Lookup(new Guid(ExtensibleStorageManager.m_VertRadiusGuid));
			if (schema == null)
			{
				return false;
			}
			Field field = schema.GetField("VertRadiusList");
			if (field == null)
			{
				return false;
			}
			Entity entity = ramp.GetEntity(schema);
			if (entity == null || entity.Schema == null)
			{
				return false;
			}
            foreach (double item in entity.Get<IList<double>>(field, (DisplayUnitType)6))
			{
				lstVertRadius.Add(item);
			}
			return true;
		}

		private static string m_WidthGuid = "D7EA64A1-BA2E-4454-B16C-8C7C9CB7A5B3";

		private static string m_VertRadiusGuid = "E9E77A4D-6DFB-4969-8CE4-F03E1F77BFF2";
	}
}
